package com.speech.utils;

import android.app.Activity;
import android.content.Context;

import com.ontbee.legacyforks.cn.pedant.SweetAlert.SweetAlertDialog;
import com.speech.MyApplication;
import com.speech.activity.LoginActivity;
import com.speech.constant.PrefsConstant;
import com.speech.db.SharedPreferenceHelper;
import com.speech.entity.BaseEntity;
import com.speech.http.BaseCallback;
import com.speech.net.BasicCallBack;
//import com.speech.net.HttpCommunicationManager;
import com.speech.net.constant.EnumReqestActionCode;
import com.speech.net.entity.MessageC2S;
import com.speech.net.entity.MessageC2SBuilder;
import com.speech.net.entity.MessageS2C;
import com.speech.net.result.UpdateInfo_Result;
import com.speech.result.Result;

import retrofit2.Call;

public class AppUpgradeUtil {

    public static void checkVersionChange(final Activity context) {


        //TODO: 接口请求 版本更新
//        MessageC2S msg_c2s = MessageC2SBuilder.buildGeneral_MessageC2S(context, EnumReqestActionCode.查询更新, null);
//        String json = JsonProcessor.StcGetJson(msg_c2s);
//        HttpCommunicationManager.getInstance().doPost(json, new BasicCallBack(context, json, new BasicCallBack.BasicCallBackListener() {
//            @Override
//            public void success(MessageS2C msg_s2c) {
//                UpdateInfo_Result updateInfo_result = JsonProcessor.StcGetObject(UpdateInfo_Result.class, msg_s2c.strData);
//
//                if (updateInfo_result != null && _check_need_update(updateInfo_result,context)) {
//                    SweetAlertDialog dialog = new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE)
//                            .setTitleText("新版本上线啦")
//                            .setContentText("请进入公众号内下载最新版本")
//                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
//                                @Override
//                                public void onClick(SweetAlertDialog sweetAlertDialog) {
//                                    sweetAlertDialog.dismiss();
//                                    MyApplication.getInstance().destory();
//                                    context.finish();
//                                }
//                            });
//                    dialog.setCancelable(false);
//                    dialog.show();
//                }
//
//            }
//
//            @Override
//            public void fail(MessageS2C msg_s2c) {
//
//            }
//        }));


        String uid,token;
        SharedPreferenceHelper spf = new SharedPreferenceHelper(context, PrefsConstant.FILE_USER_INFO);
        String lUID = (String) spf.getData(PrefsConstant.KEY_USER_INFO_LONG_UID, "");
        uid = lUID;
        String lToken = (String) spf.getData(PrefsConstant.KEY_USER_INFO_LONG_TOKEN, "");
        token = lToken;
        Call<Result<BaseEntity>> call = MyApplication.getInstance().getApiService()
                .upApp(uid,DeviceUtil.getDeviceID(context)
                        ,token,ApkVersionUtil.getVersionName(context));
        call.enqueue(new BaseCallback<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity baseEntity) {
                SweetAlertDialog dialog = new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("新版本上线啦")
                        .setContentText("请进入公众号内下载最新版本")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                sweetAlertDialog.dismiss();
                                MyApplication.getInstance().destory();
                                context.finish();
                            }
                        });
                dialog.setCancelable(false);
                dialog.show();
            }

            @Override
            public void onAbnormal(String msg) {

            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private static boolean _check_need_update(UpdateInfo_Result updateInfo_result, Activity context) {
        String[] strServerVersion = updateInfo_result.strEnforcementUpdateVersion.split("\\.");
        String[] strCurrentVersion = ApkVersionUtil.getVersionName(context).split("\\.");
        boolean bNeedUpdate = false;

        for (int index = 0; index < strServerVersion.length; index++) {
            int iServerVerison = Integer.valueOf(strServerVersion[index]);
            int iCurrentVersion = Integer.valueOf(strCurrentVersion[index]);
            if (iServerVerison > iCurrentVersion) {
                bNeedUpdate = true;
                break;
            }
        }

        return bNeedUpdate;
    }

}
